package com.bestescortbedsystem.filter;

import com.alibaba.fastjson.JSON;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: CORSAuthenticationFilter
 * Package: com.bestescortbedsystem.filter
 *
 * @author: guanzepeng
 * @Date: 2021/2/17 12:26 星期三
 * @Description: 解决shiro跨域问题 extends FormAuthenticationFilter
 */
@SuppressWarnings("AlibabaClassNamingShouldBeCamel")
public class CORSAuthenticationFilter {
//
//	private static final Logger logger = LoggerFactory.getLogger(CORSAuthenticationFilter.class);
//
//	public CORSAuthenticationFilter() {
//		super();
//	}
//
//	@Override
//	protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
//		if ("OPTIONS".equals(((HttpServletRequest) request).getMethod().toUpperCase())) {
//			return true;
//		}
//		return super.isAccessAllowed(request, response, mappedValue);
//	}
//
//	@Override
//	protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
//		HttpServletResponse res = (HttpServletResponse)response;
//		res.setHeader("Access-Control-Allow-Origin", "*");
//		res.setStatus(HttpServletResponse.SC_OK);
//		res.setCharacterEncoding("UTF-8");
//		PrintWriter writer = res.getWriter();
//		Map<String, Object> map= new HashMap<>();
//		map.put("code", 702);
//		map.put("msg", "未登录");
//		writer.write(JSON.toJSONString(map));
//		writer.close();
//		return false;
//	}
}
